package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.HoadonDAO;

/**
 * Servlet implementation class TrangHoanTatThanhToan
 */
@WebServlet("/TrangHoanTatThanhToan")
public class TrangHoanTatThanhToan extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public TrangHoanTatThanhToan() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");

		HttpSession session = request.getSession();

		String action = request.getParameter("action");
		String url = "";	
		
		if (action.equals("ThanhToan")) {

			// ---------------------
			HoadonDAO dao = new HoadonDAO();
			int maHoaDon = dao.ThemHoaDonMoi(request, session);
			// ---------------------

			request.setAttribute("maHoaDon", maHoaDon);
			session.removeAttribute("gioHang");
			
			request.setAttribute("page", "TrangHoanTatThanhToan");
			url = "../index.jsp";			
		}
		
		if (action.equals("TroLaiTrangChu")) {
			request.setAttribute("page", "TrangChu");
			url = "../index.jsp";
		}
		
		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}
}
